package com.example.tests;

import com.example.objects.SortedListOf;
import com.example.objects.User;
import com.example.page.*;
import org.junit.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TestsForMovieDataBase extends TestBase{

    @Test
	public void testCreateUser() throws Exception {
        AdminPage users_page = goToMainPage()
                .loginAsValid("admin", "123")
                .gotoUserManagement();
        SortedListOf<User> old_users = users_page.getUsers();

        User user = new User().withName("test1").withPassword("123").withEmail("test1@admin.ru");

        users_page = users_page.createNewUser(user);
        SortedListOf<User> new_users = users_page.getUsers();
        assertEquals(new_users,old_users.withAdded(user));
	}

    @Test
    public void testChangeLanguage() throws Exception
    {
        InternalPage internal_page = goToMainPage().loginAsValid("admin", "123");
        internal_page.setNederlandsLanguage();
        assertTrue("Не переключился на голландский язык",internal_page.getLanguage().equals("Nederlands"));
        internal_page.setEnglishLanguage();
        assertTrue("Не переключился на аклийский язык",internal_page.getLanguage().equals("English"));
    }

    @Test
	public void testDeleteUser_1() throws Exception {
        User user = new User().withName("admin").withPassword("123").withEmail("admin@admin.com");
        //User user = new User().withName("test1").withPassword("123").withEmail("test1@admin.ru");
        AdminPage users_page = goToMainPage()
                .loginAsValid("admin", "123")
                .gotoUserManagement();
        if (users_page.isUserExist(user))
        {
            users_page.deleteUserByName(user);
            assertFalse("Пользователь есть", users_page.isUserExist(user));
        }
        else
        {
            throw new Exception("Пользователь "+ user +" не найден");
        }
	}

    //@Test
	public void testDeleteUser_2() throws Exception {
        SortedListOf<User> old_users;
        SortedListOf<User> new_users;
        User user = new User().withName("admin").withPassword("123").withEmail("admin@admin.com");
        //User user = new User().withName("test1").withPassword("123").withEmail("test1@admin.ru");
        AdminPage users_page = goToMainPage()
                .loginAsValid("admin", "123")
                .gotoUserManagement();
        if (users_page.isUserExist(user))
        {
            old_users = users_page.getUsers();
            users_page.deleteUserByName(user);
            new_users = users_page.getUsers();
            assertEquals(new_users.withAdded(user),old_users);
        }
        else
        {
            assertTrue("Пользователь не найден " + user, users_page.isUserExist(user));
        }
	}

    @Test
    public void testOpenFilm() throws Exception
    {
        FilmsPage film_page = goToMainPage()
                .loginAsValid("admin", "123")
                .returnHomePage();
        assertTrue("Фильм не выбран", film_page.isclickOnFilm("Жизнь Пи"));
    }
}
